package com.zhiche.lisa.lspm.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.lspm.vo.common.DrivingLicenseVO;
import com.zhiche.lisa.lspm.vo.common.EnterpriseInfoVO;
import com.zhiche.lisa.lspm.vo.common.IdentityCardInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class DevopUtil {

    @Value("${devop.url}")
    private String uaaUrl;

    @Value("${devop.socketTimeout}")
    private int socketTimeout;

    @Value("${devop.drivingLicenseUrl}")
    private String drivingLicenseUrl;

    @Value("${devop.identityCardInfoUrl}")
    private String identityCardInfoUrl;

    @Value("${devop.enterpriseInfoUrl}")
    private String enterpriseInfoUrl;

    /**
     * 得到行驶证的信息
     *
     * @param imageUrl
     * @param token
     * @return
     */
    public DrivingLicenseVO getDrivingLicenseVO(String imageUrl, String token) {
        DrivingLicenseVO drivingLicenseVO = null;
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        Map<String, String> map = Maps.newHashMap();
        map.put("imgUri", imageUrl);
        String strReturn = HttpClientUtil.post(uaaUrl+drivingLicenseUrl, headNamePairs, map, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<DrivingLicenseVO> restfulResponse = JSON.parseObject(strReturn, new
                    TypeReference<RestfulResponse<DrivingLicenseVO>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                drivingLicenseVO = restfulResponse.getData();
            }
        }
        return drivingLicenseVO;
    }

    /**
     * 得到身份证信息
     *
     * @param imageUrl
     * @param type
     * @param token
     * @return
     */
    public IdentityCardInfoVO getIdentityCardInfo(String imageUrl, String type, String token) {
        IdentityCardInfoVO identityCardInfoVO = null;
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        Map<String, String> map = Maps.newHashMap();
        map.put("imgUri", imageUrl);
        map.put("type", type);
        String strReturn = HttpClientUtil.post(uaaUrl+drivingLicenseUrl, headNamePairs, map, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<IdentityCardInfoVO> restfulResponse = JSON.parseObject(strReturn, new
                    TypeReference<RestfulResponse<IdentityCardInfoVO>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                identityCardInfoVO = restfulResponse.getData();
            }
        }
        return identityCardInfoVO;
    }

    /**
     * 获取承运商信息
     *
     * @param company  公司名称
     * @param creditno 纳税人识别号
     * @return
     */
    public EnterpriseInfoVO getEnterpriseInfoUrl(String company, String creditno) {
        EnterpriseInfoVO enterpriseInfoVO = null;
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", ""));
        Map<String, String> map = Maps.newHashMap();
        map.put("company", company);
        map.put("creditno", creditno);
        String strReturn = HttpClientUtil.post(uaaUrl+enterpriseInfoUrl, headNamePairs, map, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<EnterpriseInfoVO> restfulResponse = JSON.parseObject(strReturn, new
                    TypeReference<RestfulResponse<EnterpriseInfoVO>>() {
                    });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                enterpriseInfoVO = restfulResponse.getData();
            }
        }
        return enterpriseInfoVO;
    }

}
